import { HashRouter, Routes, Route, Link } from "react-router-dom"
import { Provider } from "react-redux"
import store from "@/store/index.js"

import Layout from "@/pages/layout"
import Login from "@/pages/login"
import Dashboard from "@/pages/dashboard/index.jsx"
import GoodList from "@/pages/good/goodList.jsx"
import GoodAdd from "@/pages/good/goodForm.jsx"
import User from "@/pages/user/index.jsx"

import { asyncRoutes } from "@/pages/index.jsx"

function createRoutes(asyncRoutes) {
    let result = [];
    asyncRoutes.forEach(route => {
        if (route.path && route.element) {
            result.push(
                <Route key={route.key} path={route.path} element={route.element}></Route>
            )
        }
        if (route.children) {
            route.children.forEach(route => {
                result.push(
                    <Route key={route.key} path={route.path} element={route.element}></Route>
                )
            })
        }
    })
    return result;
}

console.log(createRoutes(asyncRoutes));

function App() {
    return (
        <HashRouter>
            {/* 就意味着子子孙孙都可以使用store了 */}
            <Provider store={store}>
                <Routes>
                    {/* 一级路由不需要出口，二级路由需要出口 */}
                    <Route path="/" element={<Layout />}>
                        {/* <Route path="/dashboard" element={<Dashboard />}></Route>
                    <Route path="/good/list" element={<GoodList />}></Route>
                    <Route path="/good/add" element={<GoodAdd />}></Route>
                    <Route path="/user" element={<User />}></Route> */}
                        {
                            createRoutes(asyncRoutes)
                        }
                    </Route>
                    <Route path="/login" element={<Login />}></Route>
                </Routes>
            </Provider>
        </HashRouter>
    );
}
export default App;
